# -*- coding: utf-8 -*-
# @Date：2024/7/18 23:31:47
# @Author：CJiaの青姝
# @FileName：serializes
# @Editor：PyCharm2024
# @Remark：
from datetime import datetime
from django.forms import model_to_dict


def model_serialize_obj(model_object):
    """
    序列化单个对象
    :param model_object: queryset对象
    :return: object对象
    """
    single = model_to_dict(model_object)
    # 创建时间
    single['create_time'] = model_object.create_time
    # 更新时间
    single['update_time'] = model_object.update_time
    # 返回对象
    return single


def model_dict_list(model_list):
    """
    序列化列表
    :param model_list: 列表
    :return:
    """
    result = []
    for model_obj in model_list:
        # queryset转字典
        obj = model_obj
        # key唯一标识
        obj['key'] = str(model_obj['id'])
        # 创建时间
        obj['create_time'] = model_obj['create_time'].strftime('%Y-%m-%d %H:%M:%S')
        # 更新时间
        obj['update_time'] = model_obj['update_time'].strftime('%Y-%m-%d %H:%M:%S')
        # # 数组追加
        result.append(obj)
    # 返回处理过的数组
    return result
